<?php

namespace qimo;
class Moor
{
    private $accountid;//账号密码
    private $secret;//账号密码

    /**
     * Moor constructor.
     * @param $accountid
     * @param $secret
     */
    public function __construct()
    {
        $this->accountid=config('qimoNumber');
        $this->secret=config('qimoAPISecret');
    }
    public function getSig()
    {
        $accountid	=	$this->accountid;//云呼编码
        $secret		=	$this->secret;//云呼密码
        $time		=	date("YmdHis");
        $sig			=	strtoupper(md5($accountid.$secret.$time));
        $authorization	=	base64_encode($accountid.":".$time);
        return [$sig,$authorization];
    }
    /**
     * 呼叫挂断
     * @param $CallID
     * @param $Exten
     * @return mixed|\Requests_Response
     */
    public function callEndData($CallID, $Exten)
    {
        list($sig,$authorization)=$this->getSig();
        $str="%s/v20160818/call/hangup/%s?sig=%s";
        $url=sprintf($str,config('qimoapi_url'),config('qimoNumber'),$sig);
        $headers = array('Content-Type' => 'application/json','Authorization'=>$authorization);
        $json=[
            'Agent'=>$Exten,
        ];
        $result= \Requests::post($url,$headers,json_encode($json));
        if (empty($result)){
            return $result;
        }
        return json_decode($result->body,true);
    }

    /**
     * 座机呼叫
     * @param $FromExten
     * @param $Exten
     * @param $ExtenType
     * @return mixed|\Requests_Response
     */
    public function seatsPost($FromExten, $Exten, $ExtenType)
    {
        list($sig,$authorization)=$this->getSig();
        $str="%s/v20160818/call/dialout/%s?sig=%s";
        $url=sprintf($str,config('qimoapi_url'),config('qimoNumber'),$sig);
        $headers = array('Content-Type' => 'application/json','Authorization'=>$authorization);
        $json=[
            'FromExten'=>$FromExten,
            'Exten'=>$Exten,
            'ExtenType'=>$ExtenType,
        ];
        $result= \Requests::post($url,$headers,json_encode($json));
        if (empty($result)){
            return $result;
        }
        return json_decode($result->body,true);
    }

    /**
     * @param $FromExten
     * @param $Exten
     * @param $ExtenType
     * @return mixed|\Requests_Response
     */
    public function setsMixPost($FromExten, $Exten, $ExtenType)
    {
        list($sig,$authorization)=$this->getSig();
        $str="%s/v20180426/rlxh/dialout/%s?sig=%s";

        $url=sprintf($str,config('qimoapi_url'),config('qimoNumber'),$sig);
        $headers = array('Content-Type' => 'application/json','Authorization'=>$authorization);
        $json=[
            'FromExten'=>$FromExten,
            'Exten'=>$Exten,
            'ExtenType'=>$ExtenType,
        ];
        $result= \Requests::post($url,$headers,json_encode($json));
        if (empty($result)){
            return $result;
        }
        return json_decode($result->body,true);
    }

    /**
     * 小号bind
     * @param $midNum
     * @param $called
     * @param $icDisplayFlag
     * @param bool $needRecord
     * @return mixed|\Requests_Response
     */
    public function callMidBind($midNum, $called, $icDisplayFlag, $needRecord=true)
    {
        list($sig,$authorization)=$this->getSig();
        $str="%s/v20160818/call/dialout/%s?sig=%s";
        $url=sprintf($str,config('qimoapi_url'),config('qimoNumber'),$sig);
        $headers = array('Content-Type' => 'application/json','Authorization'=>$authorization);
        $json=[
            'midNum'=>$midNum,
            'called'=>$called,
            'icDisplayFlag'=>$icDisplayFlag,
            'needRecord'=>$needRecord
        ];
        $result= \Requests::post($url,$headers,json_encode($json));
        if (empty($result)){
            return $result;
        }
        return json_decode($result->body,true);
    }

    /**
     * 小号解綁
     * @param $mappingId 绑定关系Id
     * @param $midNum
     * @return mixed|\Requests_Response
     */
    public function callMidUnbind($mappingId, $midNum)
    {
        list($sig,$authorization)=$this->getSig();
        $str="%s/v20160818/call/dialout/%s?sig=%s";
        $url=sprintf($str,config('qimoapi_url'),config('qimoNumber'),$sig);
        $headers = array('Content-Type' => 'application/json','Authorization'=>$authorization);
        $json=[
            'mappingId'=>$mappingId,
            'midNum'=>$midNum,

        ];
        $result= \Requests::post($url,$headers,json_encode($json));
        if (empty($result)){
            return $result;
        }

        return json_decode($result->body,true);
    }




}